package com.atcjx.controller;

import com.atcjx.MyUtils.ResultFactory;
import com.atcjx.MyUtils.TokenProccessor;

import com.atcjx.exception.UserException;
import com.atcjx.exception.exceptionEnum;
import com.atcjx.pojo.Result;
import com.atcjx.pojo.User.User;

import com.atcjx.pojo.User.UserDTO;
import com.atcjx.service.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description TODO
 * @createTime 2021年09月18日 19:04:00
 */
@Api("通用的接口")
@Controller
@CrossOrigin
public class UserController {
    @Autowired
    UserServiceImpl userService;

    @ApiOperation("登录服务")
    @ResponseBody
    @PostMapping(value = "/toLogin")
    public Result toLogin( @RequestBody UserDTO loginUser) throws UserException {
        User user = userService.login(loginUser.getUsername(),loginUser.getPassword());
        if(user==null)
            throw new UserException(exceptionEnum.ErrorUserName.getMsg(),exceptionEnum.ErrorUserName.getCode());
        if(!Objects.equals(user.getPassword(), loginUser.getPassword()))
            throw new UserException(exceptionEnum.ErrorPassWord.getMsg(),exceptionEnum.ErrorPassWord.getCode());
        //登录成功!
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", TokenProccessor.getInstance().makeToken());
        map.put("user",user);
        return ResultFactory.successWithData(map);
    }
    @ApiOperation("测试接口")
    @ResponseBody
    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String tt(){
        System.out.println("请求Test");
        return "这是一个测试窗口";
    }
}
